package com.dev;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import com.log;
public class XmlIndentEditor extends BaseIndentEditor {
	volatile char[] text;
	volatile short[] textWS;
	volatile char[] text_length;
	int primex,primey;
	int lineNumberStart;
	int tagColor=0xff0000ff;
	int stringColor=0xffff0000;
	int commentColor=0xff007f00;
	int scrollX;
	int scrollY;
	final int numColor=0x88999999;
	MainActivity ma;
    XmlIndentEditor(MainActivity ma){
		super(ma);
		setPadding((int)(6*ma.dp),8,0,0);
		setFocusableInTouchMode(true);
		textPaint.setTypeface(Typeface.MONOSPACE);
		letTextSize((int)ma.dp*ma.shr.getInt(GUISettings.NOTE_TEXT_SIZE,15));
	}
	@Override
	public void moveSelection(int a, int b) {
	}
	@Override
	public void letTextSize(int textSize) {
	}
	@Override
	public void letSelection(int a) {
	}
	@Override
	public void letSelection(int a, int b) {
	}
	void atTextChanged(){
	}
	void repl(int a,int b,char[] s){
		if(a>b){
			int n=a;
			a=b;
			b=n;
		}
		if(a<0||a>text.length||b<0||b>text.length){
			log.alert("无效的参数","selection start:"+selectionStart+"\nselection end:"+selectionEnd);
			return;
		}
		if(s!=null){
			char[] textWill=new char[text.length+s.length-(b-a)];
			for(int index=0;index<a;index++){
				textWill[index]=text[index];
			}
			for(int textWill_index=a,s_index=0;s_index<s.length;textWill_index++,s_index++){
				textWill[textWill_index]=s[s_index];
			}
			for(int org=a+s.length,offs=0;offs+org<textWill.length;offs++){
				textWill[org+offs]=text[b+offs];
			}
		}else{
			char[] textWill=new char[text.length-(b-a)];
			for(int n=0;n<a;n++){
				textWill[n]=text[n];
			}
			for(int ia=a,ib=b;ia<textWill.length&&ib<text.length;ia++,ib++){
				textWill[ia]=text[ib];
			}
		}
		invalidate();
	}
	@Override
	public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		InputConnection ic=super.onCreateInputConnection(outAttrs);
		icp=new InputConnectionProxy(ic);
		return icp;
	}
	InputConnectionProxy icp;
	class InputConnectionProxy extends BaseInputConnection{
		InputConnection former;
		InputConnectionProxy(InputConnection former){
			super(XmlIndentEditor.this,false);
			this.former=former;
		}
		@Override
		public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
			return false;
		}
		@Override
		public boolean deleteSurroundingText(int beforeLength, int afterLength) {
			int a=selectionStart;
			int b=selectionEnd;
			if(a==b){
				int start=a-beforeLength;
				int end=a;
				if(start<0)return false;
				repl(start,end,null);
				letSelection(start,start);
				start=selectionStart;
				end=selectionStart+afterLength;
				if(end>=text.length){atTextChanged();return false;}
				repl(start,end,null);
			}else{
				repl(a,b,null);
				if(a<=b){
					selectionEnd=selectionStart;
				}else{
					selectionStart=selectionEnd;
				}
			}
			atTextChanged();
			return true;
		}
		@Override
		public boolean sendKeyEvent(KeyEvent event) {
			return former.sendKeyEvent(event);
		}
		@Override
		public boolean setSelection(int start, int end) {
			return false;
		}
		@Override
		public boolean commitText(CharSequence text, int newCursorPosition) {
			String text_str=text.toString();
			if(ma.gui.ebi.num){
				text_str=text_str.replace('1','①');
				text_str=text_str.replace('2','②');
				text_str=text_str.replace('3','③');
				text_str=text_str.replace('4','④');
				text_str=text_str.replace('5','⑤');
				text_str=text_str.replace('6','⑥');
				text_str=text_str.replace('7','⑦');
				text_str=text_str.replace('8','⑧');
				text_str=text_str.replace('9','⑨');
			}
			if(ma.gui.ebi.capsLock){
				text_str=text_str.toUpperCase();
			}
			char[] text_cs=text_str.toCharArray();
			repl(selectionStart,selectionEnd,text_cs);
			if(selectionStart<selectionEnd){
				selectionEnd=selectionStart+text_cs.length;
				selectionStart=selectionEnd;
				invalidate();
			}else{
				selectionStart+=text_cs.length;
				selectionEnd=selectionStart;
				invalidate();
			}
			atTextChanged();
			return true;
		}
	}
}
